clear

*IMPORTANT: Set current directory to path of do-file before running
*cd "path of this do file"
cd "C:\Users\ngb815\Dropbox\Coercive_tech\stata\coercive_imbalance\Replication"

// OUTLINE OF THIS DO-FILE
* 1 Import data sets and construct variables
* 2 Code neighbors, second neighbors, and bilateral distances used in IV and controls for spatial spill-overs
* 3 Construct IV
* 4 Construct controls for spatial spill-overs
* 5 Construct long lags 
* 6 Merge intermediate data sets and prepare final data set for analysis

log using "C:\Users\ngb815\Dropbox\Coercive_tech\stata\coercive_imbalance\Replication\prepare_data.log"

*###############################################################################
*	1 IMPORT DATA AND CONSTRUCT MAIN VARIABLES
*###############################################################################

use "coercive_imbalance_raw\mtech_raw_jop.dta"

local categories "s a r t m h"
local stechs "1 2 3 4 5 6 7 8 9"
local atechs "1 2 3 4 5"
local rtechs "1 2 3 4 5 6"
local ttechs "1 2 3 4 5"
local mtechs "1 2"
local htechs "1 2"

// GENERATE MILITARY TECHNOLOGY INDEX FROM DATA ON INDIVIDUAL TECHNOLOGIES
gen mtech=0 if independent=="yes"
foreach x of local categories {
gen mtech_`x'=0 if independent=="yes"
foreach y of local `x'techs {
replace mtech=mtech+`x'`y'_use
replace mtech_`x'=mtech_`x'+`x'`y'_use
}
}

*Generate military technology index excluding small arms
gen mtech_exf=mtech_a+mtech_r+mtech_t+mtech_m+mtech_h
drop mtech_s mtech_a mtech_r mtech_t mtech_m mtech_h

//GENERATE LATENT INDEX 
irt 1pl a*_use r*_use t*_use m*_use h*_use if year>=1845 & independent=="yes", iterate(10)
predict index_irt_exsa if year>=1845, latent ebmeans 

irt 1pl s*_use  if year>=1845 & independent=="yes" , iterate(10) intmethod(mcaghermite)
predict index_irt_sa if year>=1845 , latent ebmeans 

//MERGE REGIONS AND COUNTRY CODE CROSSWALK
merge m:1 iso3 using "coercive_imbalance_raw/country_codes.dta", keepusing(continent* region* COW_ccode)
drop if _merge!=3
drop _merge
rename COW_ccode ccode
drop if iso3=="KOS"

// ADD WORLD BANK CLASSIFICATION OF REGIONS
gen region3=region2
replace region3="Eastern Europe and Central Asia" if country=="Kazakhstan"
replace region3="Eastern Europe and Central Asia" if country=="Kyrgyzstan"
replace region3="Eastern Europe and Central Asia" if country=="Tajikistan"
replace region3="Eastern Europe and Central Asia" if country=="Turkmenistan"
replace region3="Eastern Europe and Central Asia" if country=="Uzbekistan"
replace region3="Eastern Europe and Central Asia" if country=="Armenia"
replace region3="Eastern Europe and Central Asia" if country=="Azerbaijan"
replace region3="Eastern Europe and Central Asia" if country=="Georgia"
replace region3="Eastern Europe and Central Asia" if country=="Russian Federation"
replace region3="East Asia and the Pacific" if region3=="Asia and Oceania"
replace region3="Eastern Europe and Central Asia" if region3=="Eastern Europe and Balkan"


//MERGE GDP FROM MADDISON
rename iso3 countrycode
merge 1:1 countrycode year using "coercive_imbalance_raw/maddison_gdp.dta", keepusing(cgdppc)
gen temp=ln(cgdppc)
ipolate temp year, gen(lngdpc) by(countrycode)
drop if _merge==2
drop _merge temp cgdppc
rename countrycode iso3


// FIX CCODE TO EASE MERGE
replace ccode = 529 if country=="Ethiopia" & year>=1993
replace ccode = 818 if country=="Viet nam" & year>=1976
replace ccode = 315 if country=="Czech Republic" & year<1993
replace ccode = 342 if country=="Serbia" & year<=1920
replace ccode = 345 if country=="Serbia" & year>1920 & year<1991
replace ccode = 347 if country=="Serbia" & year>=1991 & year<2006
replace ccode = 342 if country=="Serbia" & year>=2006
replace ccode = 730 if country=="Korea, Republic of" & year<=1910
replace ccode = 769 if country=="Pakistan" & year<=1971

//MERGE DEMOCRACY DATA (BOIX ET AL. AND V-DEM)

//Boix et al
merge 1:1 ccode year using "coercive_imbalance_raw/BoixMillerRosato-v4.dta", keepusing(democracy democracy_trans) 
sort ccode year
replace country=country[_n-1] if ccode==ccode[_n-1] & country==""
replace iso3=iso3[_n-1] if ccode==ccode[_n-1] & iso3==""
replace continent=continent[_n-1] if ccode==ccode[_n-1] & continent==""
replace continent2=continent2[_n-1] if ccode==ccode[_n-1] & continent2==""
drop if country==""  | iso3==""
drop _merge
replace democracy=0 if country=="Russian Federation" & democracy==. //codes USSR as autocracy (neccessary because ccode is different in using data)
drop if country=="Serbia" & year<2010 & s1_use==. //Solves issue with having both Serbia and Serbia and Montenegro in data

//Vdem
gen country_text_id=iso3
merge 1:1 country_text_id year using "coercive_imbalance_raw/Vdem12_extract.dta", keepusing(country_text_id country_id v2x_polyarchy v2x_pubcorr v2x_corr v2x_execorr v2clrspct v2csreprss v2clkill e_total_oil_income_pc e_total_resources_income_pc)
sort country_text_id year
replace ccode=ccode[_n-1] if country_text_id==country_text_id[_n-1] & ccode==.
replace iso3=iso3[_n-1] if country_text_id==country_text_id[_n-1] & iso3==""
replace country=country[_n-1] if country_text_id==country_text_id[_n-1] & country==""
drop if country=="" | iso3==""
drop _merge

// MERGE CORRELATES OF WAR (COW) DATA

//Merge COW material capabilites
merge m:1 ccode year using "coercive_imbalance_raw/COW_NMC-60-abridged.dta", keepusing(milex milper tpop)
drop if _merge==2
drop _merge
replace milex=. if milex<0
replace milper=. if milper<0

xtset ccode year
ipolate milex year, by(ccode) gen(milex_i)
ipolate milper year, by(ccode) gen(milper_i)
replace milex=milex_i
replace milper=milper_i
drop milex_i milper_i

//Merge COW interstate wars
preserve
clear
use "coercive_imbalance_raw/COW_interstate_wars.dta"
expand 195
sort warnum ccode
egen year=seq(), by(warnum ccode side)
replace year=year+1815

gen interstatewar=1 if year>=startyear1 & year<=endyear1
replace interstatewar=1 if year>=startyear2 & year<=endyear2 & endyear2!=.

collapse (max) interstatewar, by(ccode year) 

save "coercive_imbalance_final/temp.dta", replace
restore

merge 1:1 ccode year using "coercive_imbalance_final/temp.dta"
drop if _merge==2
drop _merge
replace interstatewar=0 if interstatewar==. & tpop!=. //code 0 if no war, missing if not in CoW data


//Merge COW intrastate conflicts
preserve
clear
use "coercive_imbalance_raw/COW_intrastatewar_v51.dta"
expand 195
rename CcodeA ccode
sort WarNum ccode
egen year=seq(), by(WarNum ccode)
replace year=year+1815
 
gen civilwar=1 if year>=StartYr1 & year<=EndYr1
replace civilwar=1 if year>=StartYr2 & year<=EndYr2 & EndYr2!=.
replace civilwar=1 if year>=StartYr3 & year<=EndYr3 & EndYr3!=.
replace civilwar=1 if year>=StartYr4 & year<=EndYr4 & EndYr4!=.

gen civilwar_central=civilwar if WarType==4

collapse (max) civilwar*, by(ccode year) 

save "coercive_imbalance_final\temp.dta", replace
restore

merge 1:1 ccode year using "coercive_imbalance_final/temp.dta"
drop if _merge==2
drop _merge
replace civilwar=0 if civilwar==.
replace civilwar_central=0 if civilwar_central==. & tpop!=. //code 0 if no war, missing if not in CoW data


//Merge COW international trade
merge m:1 ccode year using "coercive_imbalance_raw/COW_trade_4.dta", keepusing(imports exports)
drop if _merge==2
drop _merge

replace imports=. if imports==-9
replace exports=. if exports==-9
gen temp=ln(imports+exports) if imports!=. & exports!=.
ipolate temp year, gen(lntrade) by(ccode)
drop temp
gen trade=exp(lntrade)


//Merge COW Alliances
preserve
clear
use "coercive_imbalance_raw/COW_alliances.dta"
gen ally_USA=1 if state_name1=="United States of America" 
gen ally_Russia=1 if state_name1=="Russia"

collapse (mean) ally_*, by(ccode2 year)
rename ccode2 ccode
save "coercive_imbalance_final/temp.dta", replace
restore

merge 1:1 ccode year using "coercive_imbalance_final/temp.dta"
drop if _merge==2
drop _merge

replace ally_USA=0 if ally_USA==.
replace ally_Russia=0 if ally_Russia==.
replace ally_USA=1 if country=="United States"
replace ally_Russia=1 if country=="Russian Federation"

//MERGE FOREIGN AID DATA
gen countrycode=iso3
merge 1:1 countrycode year using "coercive_imbalance_raw/aid_dac_countries.dta"
gen lnaid=ln(aid)
replace lnaid=0 if lnaid==.
drop if _merge==2
drop _merge

//MERGE COLONIAL RELATIONSHIPS FROM CEPII
preserve
clear
use "coercive_imbalance_raw/dist_cepii.dta"
rename iso_o iso3
gen colony_we=colony if iso_d=="FRA" | iso_d=="DEU" | iso_d=="GBR"  | iso_d=="PRT"  | iso_d=="NLD"  | iso_d=="ITA" 
replace colony_we=0 if colony_we==.
gen colony_ger=colony if iso_d=="DEU" 
replace colony_ger=0 if colony_ger==.
gen colony_fra=colony if iso_d=="FRA" 
replace colony_fra=0 if colony_fra==.
gen colony_uk=colony if iso_d=="GBR" 
replace colony_uk=0 if colony_uk==.
gen colony_ussr=colony if iso_d=="RUS"
replace colony_ussr=0 if colony_ussr==.
keep iso3 colony colony_we colony_ger colony_fra colony_uk colony_ussr
collapse (max) colony*, by(iso3)
replace iso3="ROU" if iso3=="ROM"
replace iso3="SRB" if iso3=="YUG"
save "coercive_imbalance_final\temp.dta", replace
restore

merge m:1 iso3 using "coercive_imbalance_final/temp.dta"
drop if _merge==2
drop _merge
replace colony=0 if colony==.
replace colony_we=0 if colony_we==.
replace colony_ger=0 if colony_ger==.
replace colony_fra=0 if colony_fra==.
replace colony_uk=0 if colony_uk==.
replace colony_ussr=0 if colony_ussr==.
gen colony_rest=0  
replace colony_rest=1 if colony_we==1 & colony_fra!=1 & colony_uk!=1 & colony_ger!=1  


gen temp1=1 if democracy!=. & year==1946 //code independence in 1946, colony variables only consider postwar period
egen temp2=max(temp1), by(iso3)
replace colony=0 if temp2==1 | country=="Germany" | country=="Israel" | country=="Taiwan, Province of China"
replace colony_we=0 if temp2==1 | country=="Germany" | country=="Israel" | country=="Taiwan, Province of China"
replace colony_ger=0 if temp2==1 | country=="Germany" | country=="Israel" | country=="Taiwan, Province of China"
replace colony_fra=0 if temp2==1 | country=="Germany" | country=="Israel" | country=="Taiwan, Province of China"
replace colony_uk=0 if temp2==1 | country=="Germany" | country=="Israel" | country=="Taiwan, Province of China"
replace colony_ussr=0 if temp2==1
drop temp*


//MERGE HYDE POPULATION DATA
merge 1:1 iso3 year using "coercive_imbalance_raw/hyde_pop.dta", keepusing(pop_hyde)

replace pop_hyde=tpop*1000 if country=="Maldives" //only country with missing data in hyde
replace pop_hyde=42380 if country=="Maldives" & year==1820 //data from clio infra
replace pop_hyde=72000 if country=="Maldives" & year==1910

replace pop_hyde=ln(pop_hyde)
ipolate pop_hyde year, by(iso3) gen(temp)
replace pop_hyde=exp(temp)

drop if _merge==2
drop _merge temp

merge 1:1 iso3 year using "coercive_imbalance_raw/hyde_upop.dta", keepusing(upop_hyde)
drop if _merge==2
drop _merge 

gen ups=upop_hyde/pop_hyde

//MERGE AES
rename iso3 iso
merge 1:1 iso year using "coercive_imbalance_raw/aes.dta", keepusing(aes)
drop if _merge==2
drop _merge 
rename iso iso3

gen naes=1-aes


//MERGE EDUCATION
gen country_temp=country
replace country="Bolivia" if country=="Bolivia, Plurinational State of"
replace country="Congo, D.R." if country=="Congo, the Democratic Republic of the"
replace country="Cote dIvoire" if country=="Cote D'Ivoire"
replace country="Dominican Rep." if country=="Dominican Republic"
replace country="Iran" if country=="Iran, Islamic Republic of"
replace country="Republic of Korea" if country=="Korea, Republic of"
replace country="Syria" if country=="Syrian Arab Republic"
replace country="Taiwan" if country=="Taiwan, Province of China"
replace country="USA" if country=="United States"
replace country="Venezuela" if country=="Venezuela, Bolivarian Republic of"

merge 1:1 country year using  "coercive_imbalance_raw/lee_lee_education.dta"
drop if _merge==2
drop _merge country
rename country_temp country

replace schooling=ln(schooling)
ipolate schooling year, by(iso3) gen(temp)
replace schooling=exp(temp)
drop temp

//MERGE INFORMATION CAPACITY VARIABLES
rename ccode ccodecow
sort ccodecow year
merge 1:1 ccodecow year using "coercive_imbalance_raw/information_capacity.dta" , keepusing(yearbook_published census)
rename ccodecow ccode 
drop if _merge==2
drop _merge

//MERGE DATA ON DIPLOMACY AND UNIVERSITIES
sort iso3 year
merge m:1 iso3 using "coercive_imbalance_raw/atech_raw.dta" , keepusing(adoptionyearn05 adoptionyearn06 )
rename adoptionyearn05 Diplomacy
rename adoptionyearn06 University
drop if _merge==2
drop _merge

//GENERATE ADDITIONAL VARIABLES
xtset ccode year
sort ccode year

gen gdp=exp(lngdpc)+pop_hyde
gen lnmilex=ln(milex)
gen lnmilex_pop=ln(milex)-ln(tpop)
gen milper_pop=milper/tpop
gen civilwar_3y=max(civilwar, L.civilwar, L2.civilwar)
gen civilwar_central_3y=max(civilwar_central, L.civilwar_central, L2.civilwar_central)
gen interstatewar_3y=max(interstatewar, L.interstatewar, L2.interstatewar)
gen lnmilper=ln(milper)
gen coldwar=0
replace coldwar=1 if year>1946 & year<1992
gen le_total_oil_income_pc=ln(e_total_oil_income_pc+0.001)
gen le_total_resources_income_pc=ln(e_total_resources_income_pc+0.001)
save "coercive_imbalance_final/ci_main_jop.dta", replace

erase "coercive_imbalance_final/temp.dta"

*###############################################################################
*	2 CODE NEIGHBORS AND DISTANCES
*###############################################################################

*This bit of code identifies neighbours, second neighbors and bilateral distances used to
*cosntruct the instrument and the controls for spatial spill-overs

*Get neighbours
clear
use "coercive_imbalance_raw/COW_contiguity_2010.dta"


keep if conttype<4
keep state*no


save "coercive_imbalance_final/temp.dta", replace

rename state1no ccode
rename state2no state1no

joinby state1no using "coercive_imbalance_final/temp.dta"

expand 2, gen(new)
gen neighbour=state1no if new==0
gen neighbour_type=1 if new==0
replace neighbour=state2no if new==1
replace neighbour_type=2 if new==1
sort ccode neighbour neighbour_type

duplicates drop ccode neighbour, force

keep ccode neighbour neighbour_type 
save "coercive_imbalance_final/neighbours_jop.dta", replace

*Get bilateral distances
clear
use "coercive_imbalance_raw/dist_cepii.dta" 
keep iso* distw

replace iso_o="ROU" if iso_o=="ROM"
replace iso_d="ROU" if iso_d=="ROM"
replace iso_o="SRB" if iso_o=="YUG"
replace iso_d="SRB" if iso_d=="YUG"

rename iso_o iso3

merge m:1 iso3 using "coercive_imbalance_raw/country_codes.dta", keepusing(country COW_ccode region2)
drop if _merge!=3
drop _merge

rename COW_ccode ccode
rename iso3 iso_main
rename iso_d iso3
rename region2 region

merge m:1 iso3 using "coercive_imbalance_raw/country_codes.dta", keepusing(COW_ccode region2)
drop if _merge!=3
drop _merge
rename COW_ccode neighbour
rename iso3 iso3_dyad
rename iso_main iso3
rename region2 region_neighbour

duplicates drop ccode neighbour, force

*merge back neighbour data on distances
merge 1:1 ccode neighbour using "coercive_imbalance_final/neighbours_jop.dta", keepusing(neighbour_type)
drop if _merge==2
drop _merge ccode neighbour
order country iso3 iso3_dyad neighbour_type distw

drop if iso3==iso3_dyad

gen year=2000
merge m:1 iso3 year using "coercive_imbalance_final/ci_main_jop.dta"
drop if _merge!=3
keep country iso3 iso3_dyad neighbour_type distw

drop if distw==.

save "coercive_imbalance_final/neighbours_jop.dta", replace
erase "coercive_imbalance_final/temp.dta"



*###############################################################################
*	3 CONSTRUCT INSTRUMENT
*###############################################################################

*This bit of code constructs the instrument (and the alternative instruments used in the appendix)

clear
use "coercive_imbalance_final/ci_main_jop.dta"

replace independent="" if democracy==. // restrict sample to countries covered by both the Boix et al democracy data (equivalent to keeping only independent states) and the military tech data

keep country year iso3 pop_hyde ccode *_use independent 
drop if year<1820 


//Reshape
rename *_use use*
rename uses* use1*
rename usea* use2*
rename user* use3*
rename uset* use4*
rename usem* use5*
rename useh* use6*

reshape long use, i(country year iso3 pop_hyde independent) j(tech)


//SAVE MEMORY
//Drop observations prior to invention
gen temp=year if use==1
egen firstuse=min(temp), by(tech)
drop if year<firstuse-1
drop temp


*drop observations five years after full adoption
egen adoptionrate=mean(use), by(tech year)
gen temp=year if adoptionrate==1
egen lastyear=min(temp), by(tech)
drop if year>lastyear+5
drop lastyear temp

drop if iso3==""


save "coercive_imbalance_final/temp.dta", replace
 
//GENERATE VARIABLES
levelsof tech, local(techs)
foreach x of local techs {
*foreach x of numlist 31 { // for debugging
clear
use "coercive_imbalance_final/temp.dta"
keep if tech==`x'
sum year
scalar firstyear=`r(min)'
scalar lastyear=`r(max)'
clear
use "coercive_imbalance_final/neighbours_jop.dta"
expand lastyear-firstyear+1
egen dyad=group(iso3 iso3_dyad)
sort dyad
gen year=_n-(dyad-1)*(lastyear-firstyear+1)+firstyear-1
drop dyad
gen tech=`x'
merge m:1 iso3 year tech using "coercive_imbalance_final/temp.dta", keepusing(independent) //eliminate observations when main country not independent to save time
drop if _merge!=3
drop _merge
rename iso3 iso3_main
rename iso3_dyad iso3
drop if independent!="yes"
drop independent
merge m:1 iso3 year tech using "coercive_imbalance_final/temp.dta" //import use, population data for weights etc
drop if _merge!=3
drop _merge
rename iso3 iso3_neighbour
rename iso3_main iso3

gen bal_temp=1 if year==lastyear & use==.
egen missing=max(bal_temp), by(iso3_neighbour)
drop if missing==1

egen obsno=count(year), by(iso3 iso3_neighbour)
gen obs_dyad=1 if independent=="yes"
egen obsno_dyad=sum(obs_dyad), by(iso3 iso3_neighbour)
egen yindp=min(year) , by(iso3)
gen balance_indp=1 if obsno==obsno_dyad 


*gen lnpop in startyear
gen temp=ln(pop_hyde) if year==firstyear
egen lnpop_firstyear=mean(temp), by(iso3_neighbour iso3)
drop temp
gen temp=ln(pop_hyde) if year==max(yindp,firstyear)
egen lnpop_yindp=mean(temp), by(iso3_neighbour iso3)
drop temp

             
*Generate variables (all both simple and ex neighbours): distance to nearest user , use in neighbours, distance-weighted use 

drop if iso3_neighbour=="DEU" | iso3_neighbour=="FRA" | iso3_neighbour=="RUS" | iso3_neighbour=="GBR" | iso3_neighbour=="USA"

gen distweight_use1=use/distw 
gen distweight_use2=use/distw if neighbour_type!=1 

gen distweight_use1_bali=use/distw if balance_indp==1
gen distweight_use2_bali=use/distw if neighbour_type!=1 & balance_indp==1
gen distweight_use3_bali=use/distw if neighbour_type!=1 & balance_indp==1 & distw>500
gen distweight_use4_bali=use/distw if neighbour_type!=1 & balance_indp==1 & distw>750
gen distweight_use5_bali=use/distw if neighbour_type!=1 & balance_indp==1 & distw>1000


gen distweight_obs1=1/distw
gen distweight_obs2=1/distw if neighbour_type!=1

gen distweight_obs1_bali=1/distw if balance_indp==1
gen distweight_obs2_bali=1/distw if neighbour_type!=1 & balance_indp==1
gen distweight_obs3_bali=1/distw if neighbour_type!=1 & balance_indp==1 & distw>500
gen distweight_obs4_bali=1/distw if neighbour_type!=1 & balance_indp==1 & distw>750
gen distweight_obs5_bali=1/distw if neighbour_type!=1 & balance_indp==1 & distw>1000


gen distweight_usepop1_bali=distweight_use1_bali*lnpop_yindp
gen distweight_obspop1_bali=distweight_obs1_bali*lnpop_yindp
gen distweight_usepop2_bali=distweight_use2_bali*lnpop_yindp
gen distweight_obspop2_bali=distweight_obs2_bali*lnpop_yindp


collapse (sum) distw* , by(iso3 tech country year)


cd coercive_imbalance_final
save temp`x'.dta , replace
cd ..

}


*Append
clear
use "coercive_imbalance_final/temp11.dta"

drop if year!=9999

cd coercive_imbalance_final
*foreach x of local techs {
foreach x of numlist 11 12 13 14 15 16 17 18 19 21 22 23 24 25 31 32 33 34 35 36 41 42 43 44 45 51 52 61 62 {
append using temp`x'.dta
erase temp`x'.dta
}
cd ..


save "coercive_imbalance_final/temp.dta", replace

//Merge use data
clear
use "coercive_imbalance_final/ci_main_jop.dta"
drop if independent!="yes"
keep country year iso3 region2 ccode *_use



//Reshape
rename *_use use*
rename uses* use1*
rename usea* use2*
rename user* use3*
rename uset* use4*
rename usem* use5*
rename useh* use6*

reshape long use, i(country year iso3 region2 ) j(tech)


merge 1:1 iso3 tech year using "coercive_imbalance_final/temp.dta"
drop if _merge==2
drop _merge

save "coercive_imbalance_final/temp.dta", replace

*Generate final IVs
clear
use "coercive_imbalance_final/temp.dta"
set matsize 1000

gen iv_n1=distweight_use1_bali/distweight_obs1_bali
gen iv_n2=distweight_use2_bali/distweight_obs2_bali
gen iv_n1_pop=distweight_usepop1_bali/distweight_obspop1_bali
gen iv_n2_pop=distweight_usepop2_bali/distweight_obspop2_bali
gen iv_n2_500=distweight_use3_bali/distweight_obs3_bali
gen iv_n2_750=distweight_use4_bali/distweight_obs4_bali
gen iv_n2_1000=distweight_use5_bali/distweight_obs5_bali


*Fill out missing observations before invention year and after year where all countries have adopted
gen temp=year if use==1
egen firstobs=min(temp), by(tech)
drop temp
gen temp=year if use==0
egen lastobs=max(temp), by(tech)

foreach x in iv_n1_pop iv_n2_pop iv_n1 iv_n2 iv_n2_500 iv_n2_750 iv_n2_1000 {
replace `x'=0 if year<firstobs
replace `x'=1 if year>lastobs
}

drop temp firstobs lastobs

*Reshape 
keep country iso3 tech iv_* year 
reshape wide iv_* , i(country iso3 year) j(tech)
foreach x in iv_n1_pop iv_n2_pop iv_n1 iv_n2 iv_n2_500 iv_n2_750 iv_n2_1000 {
rename `x'1* s*_`x'
rename `x'2* a*_`x'
rename `x'3* r*_`x'
rename `x'4* t*_`x'
rename `x'5* m*_`x'
rename `x'6* h*_`x'
}

*Generate IV version of various permutations of the military technology index
local categories "s a r t m h"
local stechs "1 2 3 4 5 6 7 8 9"
local atechs "1 2 3 4 5"
local rtechs "1 2 3 4 5 6"
local ttechs "1 2 3 4 5"
local mtechs "1 2"
local htechs "1 2"

foreach z in iv_n1_pop iv_n2_pop iv_n1 iv_n2 iv_n2_500 iv_n2_750 iv_n2_1000 {
gen mtech_`z'=0
foreach x of local categories {
gen mtech_`x'_`z'=0
foreach y of local `x'techs {
replace mtech_`z'=mtech_`z'+`x'`y'_`z'
replace mtech_`x'_`z'=mtech_`x'_`z'+`x'`y'_`z'
}
}
gen mtech_exf_`z'=mtech_a_`z'+mtech_r_`z'+mtech_t_`z'+mtech_m_`z'+mtech_h_`z'

}

keep country year iso3 mtech_iv_n2_pop mtech_iv_n1 mtech_iv_n2 mtech_exf_iv_n2 mtech_iv_n2_500 mtech_iv_n2_750 mtech_iv_n2_1000  mtech_exf_* m2_iv_n2 r3_iv_n2



save "coercive_imbalance_final/instruments_jop.dta", replace
erase "coercive_imbalance_final/temp.dta"


*###############################################################################
*	4 CONSTRUCT CONTROLS FOR SPATIAL SPILL_OVERS
*###############################################################################

*This bit of code construct variables in neighboring countries as well as distance-weighted control variables

clear
use "coercive_imbalance_final\neighbours_jop.dta"

local dummies "interstatewar democracy civilwar ally_USA ally_Russia " 
local cvars "gdp trade aid pop_hyde mtech index_irt_exsa"

local allvars `dummies' `cvars'

expand 196
egen year=seq(), by(iso3 iso3_dyad)
replace year=year+1819

merge m:1 iso3 year using "coercive_imbalance_final/ci_main_jop.dta", keepusing(democracy region3)
drop if _merge!=3
drop if democracy==.
keep iso3 iso3_dyad neighbour_type distw year region3 country

rename iso3 iso3_main
rename iso3_dyad iso3
rename region3 region3_main

merge m:1 iso3 year using "coercive_imbalance_final/ci_main_jop.dta", keepusing(`allvars' independent region3)
drop if _merge!=3
drop _merge

drop if independent!="yes"

rename iso3 iso3_neighbour
rename iso3_main iso3

gen majorpower=1 if country=="United Kingdom" | country=="France" | country=="Germany" | country=="United States" | country=="Russian Federation"


foreach x of local allvars {
gen n1_`x'=`x' if neighbour_type==1
gen n2_`x'=`x' if neighbour_type==2
}


*Generate # of neighbors
gen n1number=1 if neighbour_type==1
gen n2number=1 if neighbour_type==2

*gen distances excluding neighbors
gen distw2=distw if neighbour_type!=1

*Generate distance-weighted variables
foreach x of local allvars {
gen distw1_`x'=`x'/distw if majorpower!=1
gen distw1_`x'_obs=1/distw if `x'!=. & majorpower!=1
gen distw2_`x'=`x'/distw2 if majorpower!=1
gen distw2_`x'_obs=1/distw2 if `x'!=. & majorpower!=1


}


*Collapse dyadic data
keep *dist* n1* n2* iso3 year 
collapse (sum) *dist* n1* n2* , by(iso3 year)

*Generate averages
gen n1_democracy_avg= n1_democracy/n1number if n1number!=0
gen n2_democracy_avg= n2_democracy/n2number if n2number!=0
replace n1_democracy_avg=0 if n1number==0 & n1_democracy!=.
replace n2_democracy_avg=0 if n2number==0 & n2_democracy!=.


*Create dummies from sums
foreach x of local dummies {
replace n1_`x'=0 if n1_`x'==.
replace n2_`x'=0 if n2_`x'==.
replace n1_`x'=1 if n1_`x'>1 
replace n2_`x'=1 if n2_`x'>1 

}

*Generate log variables
foreach x of local cvars {
gen n1_ln`x'=ln(n1_`x') 
gen n2_ln`x'=ln(n2_`x')
gen distw1_ln`x'=ln(distw1_`x')
gen distw2_ln`x'=ln(distw2_`x')
}
 

*Rescale weighted vars
foreach x of local allvars {
replace distw1_`x'=distw1_`x'/distw1_`x'_obs
replace distw2_`x'=distw2_`x'/distw2_`x'_obs
}

drop *_obs n1number n2number  *hyde* *lnmtech

save "coercive_imbalance_final/diffusioncontrols_jop.dta", replace

*###############################################################################
*	5 CONSTRUCT LONG LAGS (FOR FIGURE 1)
*###############################################################################
clear
use "coercive_imbalance_raw\mtech_raw_jop.dta"

local categories "s a r t m h"
local stechs "1 2 3 4 5 6 7 8 9"
local atechs "1 2 3 4 5"
local rtechs "1 2 3 4 5 6"
local ttechs "1 2 3 4 5"
local mtechs "1 2"
local htechs "1 2"

// LOAD DATA
*mtech
gen int mtech=0 if independent=="yes"
foreach x of local categories {
gen mtech_`x'=0 if independent=="yes"
foreach y of local `x'techs {
replace mtech=mtech+`x'`y'_use
replace mtech_`x'=mtech_`x'+`x'`y'_use
}
}
gen int mtech_exf=mtech_a+mtech_r+mtech_t+mtech_m+mtech_h if independent=="yes"
drop mtech_a mtech_r mtech_t mtech_m mtech_h

drop *_use 

*population size (urban and total)
merge 1:1 iso3 year using "coercive_imbalance_raw/hyde_pop.dta", keepusing(pop_hyde)
drop _merge 

merge 1:1 iso3 year using "coercive_imbalance_raw/hyde_upop.dta", keepusing(upop_hyde)
drop _merge 

replace pop_hyde=ln(pop_hyde)
ipolate pop_hyde year, by(iso3) gen(temp)
replace pop_hyde=exp(temp)
drop temp

replace upop_hyde=ln(upop_hyde)
ipolate upop_hyde year, by(iso3) gen(temp)
replace upop_hyde=exp(temp)
drop temp

gen ups=upop_hyde/pop_hyde

*agricultural employment share
rename iso3 iso
merge 1:1 iso year using "coercive_imbalance_raw/aes.dta", keepusing(aes)
drop _merge 
rename iso iso3

gen naes=1-aes

//GDP per capita
rename iso3 countrycode
merge 1:1 countrycode year using "coercive_imbalance_raw/maddison_gdp.dta", keepusing(cgdppc)
gen temp=ln(cgdppc)
ipolate temp year, gen(lngdpc) by(countrycode)
drop _merge temp cgdppc
rename countrycode iso3

egen temp=mode(country), by(iso3)
replace country=temp
drop temp
drop if country==""

egen temp=mode(iso3), by(country)
replace iso3=temp
drop temp
drop if iso3==""


*education
gen country_temp=country
replace country="Bolivia" if country=="Bolivia, Plurinational State of"
replace country="Congo, D.R." if country=="Congo, the Democratic Republic of the"
replace country="Cote dIvoire" if country=="Cote D'Ivoire"
replace country="Dominican Rep." if country=="Dominican Republic"
replace country="Iran" if country=="Iran, Islamic Republic of"
replace country="Republic of Korea" if country=="Korea, Republic of"
replace country="Syria" if country=="Syrian Arab Republic"
replace country="Taiwan" if country=="Taiwan, Province of China"
replace country="USA" if country=="United States"
replace country="Venezuela" if country==" Venezuela, Bolivarian Republic of"

merge 1:1 country year using  "coercive_imbalance_raw/lee_lee_education.dta"
drop if _merge==2
drop _merge country
rename country_temp country

replace schooling=ln(schooling)
ipolate schooling year, by(iso3) gen(temp)
replace schooling=exp(temp)
drop temp


tab iso3, missing

*region identifiers
merge m:1 iso3 using "coercive_imbalance_raw/country_codes.dta", keepusing(continent* region*)
drop if _merge!=3
drop _merge
drop if iso3=="KOS"



//GENERATE WESTERN EUROPE DATA
local outcomes "mtech mtech_exf mtech_s lngdpc ups naes schooling" 
preserve
*balance sample (except that aes data missing for Sweden, Switzerland)
keep if country=="Belgium" | country=="Switzerland" |country=="Spain" | country=="France" | country=="United Kingdom" | country=="Italy" | country=="Portugal" | country=="Sweden" 
replace naes=. if country=="Switzerland" | country=="Sweden"

sort iso3 year

collapse (mean) `outcomes', by(year)

replace mtech=round(mtech)
replace mtech_exf=round(mtech_exf)
replace mtech_s=round(mtech_s)

sort year
scalar z=1
local num=z
foreach x of varlist `outcomes' {
foreach y of numlist 1400/2010 {
replace `x'=`x'[_n-1] if `x'[_n-1]>`x' & year==`y' & `x'[_n-1]!=.
}
rename `x' value`num'
scalar z=z+1
local num=z
}



reshape long value, i(year) j(metric)
rename year year_we
reshape wide value, i(year) j(metric)



local i=0
foreach x of local outcomes {
local i=`i'+1
rename *`i' `x'
}

drop if year_we<1400
set obs 651
replace year_we=year_we[_n-1]+1 if year_we==.
foreach x of varlist `outcomes' {
replace `x'=`x'[_n-1]+(`x'[_n-1]-`x'[_n-11])/10 if 	`x'==.
}

replace mtech=floor(mtech)
replace mtech_exf=floor(mtech_exf)
replace mtech_s=floor(mtech_s)


*make sure all values are unique (or merge will fail). Duplicates are given negative values. Most recent year with outcome=X is chosen as year_we for X.
foreach x of varlist `outcomes' {
replace `x'=`x'[_n-1] if `x'[_n-1]>`x' &  `x'[_n-1]!=.
replace `x'=-_n if `x'[_n+1]==`x'
replace `x'=-_n if `x'==.
}

save "coercive_imbalance_final/temp_we.dta", replace
restore
 
drop if year<1850

*nearmerge

local i=0
foreach x of varlist `outcomes' {
local i=`i'+1
*for discrete vars
if `i'<=3 {
merge m:1 `x' using	"coercive_imbalance_final\temp_we.dta", keepusing(year_we)
rename year_we year_we_current
replace `x'=`x'-1
drop _merge
merge m:1 `x' using	"coercive_imbalance_final\temp_we.dta", keepusing(year_we)
rename year_we year_we_previous
gen lagwe_`i'=year-year_we_current if year_we_current<year
replace lagwe_`i'=0 if year_we_current>=year & year_we_previous<=year //set lag to zero if tech same as WE
replace lagwe_`i'=year-year_we_previous+1 if  year_we_current>year & year_we_previous>year //set lag to negative if ahead of WE
drop year_we_current year_we_previous _merge
replace `x'=`x'+1
}
*for continuous vars
if `i'>3 {
nearmrg using "coercive_imbalance_final\temp_we.dta", nearvar(`x') type(m:1) keepusing(year_we)
replace year_we=. if `x'==.
gen lagwe_`i'=year-year_we
drop _merge year_we
}
}

local i=0
foreach x of local outcomes {
local i=`i'+1
rename lagwe_`i' lagwe_`x'
}

*ad hoc adjustment for mtech_s (it reaches maximum in 1958, leading to flawed adoption lags for countries reaching the maximum after that date)
replace lagwe_mtech_s=0 if year>=1958 & mtech_s==9


sort iso3 year
keep iso3 year lagwe* 
drop if iso3==""


*merge iv
merge 1:1 iso year using "coercive_imbalance_final/instruments_jop.dta", keepusing(mtech_exf_iv_n2 mtech_exf_iv_n2_500 mtech_exf_iv_n2_750 mtech_exf_iv_n2_1000 mtech_exf_iv_n2_pop mtech_exf_iv_n1)
drop if _merge==2
drop _merge

local lagivs "mtech_exf_iv_n2 mtech_exf_iv_n2_500 mtech_exf_iv_n2_750 mtech_exf_iv_n2_1000 mtech_exf_iv_n2_pop mtech_exf_iv_n1"

foreach x of local lagivs {

*low
gen mtech_exf=floor(`x')
merge m:1 mtech_exf using	"coercive_imbalance_final\temp_we.dta", keepusing(year_we)
rename year_we year_we_current
replace mtech_exf=mtech_exf-1
drop _merge
merge m:1 mtech_exf using	"coercive_imbalance_final\temp_we.dta", keepusing(year_we)
rename year_we year_we_previous
gen lagwe_low=year-year_we_current if year_we_current<year
replace lagwe_low=0 if year_we_current>=year & year_we_previous<=year //set lag to zero if tech same as WE
replace lagwe_low=year-year_we_previous+1 if  year_we_current>year & year_we_previous>year //set lag to negative if ahead of WE
drop mtech_exf _merge year_we_previous year_we_current

*high
gen mtech_exf=ceil(`x')
merge m:1 mtech_exf using	"coercive_imbalance_final\temp_we.dta", keepusing(year_we)
rename year_we year_we_current
replace mtech_exf=mtech_exf-1
drop _merge
merge m:1 mtech_exf using	"coercive_imbalance_final\temp_we.dta", keepusing(year_we)
rename year_we year_we_previous
gen lagwe_high=year-year_we_current if year_we_current<year
replace lagwe_high=0 if year_we_current>=year & year_we_previous<=year //set lag to zero if tech same as WE
replace lagwe_high=year-year_we_previous+1 if  year_we_current>year & year_we_previous>year //set lag to negative if ahead of WE
drop mtech_exf _merge year_we_previous year_we_current

*weighted average of low and high
gen lagwe_`x'=lagwe_high if floor(`x')==ceil(`x')
replace lagwe_`x'=(`x'-floor(`x'))*lagwe_high+(ceil(`x')-`x')*lagwe_low if lagwe_`x'==.
drop `x' lagwe_low lagwe_high



}

drop if iso3==""
keep  iso3 year lagwe_mtech lagwe_mtech_exf_iv_n2 lagwe_mtech_exf_iv_n1 lagwe_mtech_exf_iv_n2_500 lagwe_mtech_exf_iv_n2_750 lagwe_mtech_exf_iv_n2_1000 lagwe_mtech_exf_iv_n2_pop   lagwe_mtech_s   lagwe_mtech_exf lagwe_mtech_s lagwe_lngdpc lagwe_mtech_s   lagwe_naes lagwe_ups lagwe_schooling

sort iso year
save "coercive_imbalance_final\temp_lags.dta", replace
 



*###############################################################################
*	6 CREATE FINAL DATA SET FOR ANALYSIS
*###############################################################################

*This bit of code merges the intermediate data sets constructed above, generates
*a few addional variables, deletes unneccessary ones, and renames the remaining. 
*The final data set is saved in the folder data.

clear
use "coercive_imbalance_final/ci_main_jop.dta"

merge 1:1 iso3 year using "coercive_imbalance_final/instruments_jop.dta"
drop if _merge==2
drop _merge

merge 1:1 iso3 year using "coercive_imbalance_final/diffusioncontrols_jop.dta"
drop if _merge==2
drop _merge

merge 1:1 iso3 year using "coercive_imbalance_final/temp_lags.dta"
drop if _merge==2
drop _merge


egen countryno=group(iso3)

xtset countryno year
sort countryno year

local lagvars "interstatewar civilwar " 
foreach x of local lagvars {
gen n1_`x'_3y=max(n1_`x', L.n1_`x', L2.n1_`x')
gen n2_`x'_3y=max(n2_`x', L.n2_`x', L2.n2_`x')
}

drop if year<1820

*Drop unnecessary variables (e.g., GDP in levels). Some of these variables are auxillary variables used to create final variables,
*others are redundant because they are so closely related to variables we already control for
drop region region3 continent country_text_id
drop countrycode series imports exports trade aid gdp *_trade *_aid *_gdp pop_hyde upop_hyde   n1_lngdp n2_lngdp mtech_iv_* 
drop distw1_ally_USA distw1_ally_Russia  distw1_mtech 
drop distw2_ally_USA distw2_ally_Russia  distw2_mtech 
drop n2_ally_USA n2_ally_Russia  n2_mtech   n1_mtech n1_lntrade n2_lntrade n1_lnaid  n2_lnaid
drop  n1_civilwar_3y  n2_civilwar_3y n1_interstatewar_3y n2_interstatewar_3y lnmilex_pop milper_pop civilwar_central_3y 
drop n1_lnindex_irt_exsa n2_lnindex_irt_exsa  distw1_lnindex_irt_exsa distw2_lnindex_irt_exsa distw1_lntrade  distw2_lntrade distw1_lnaid distw2_lnaid distw2_lngdp  mtech_exf_iv_n1_pop mtech_exf_iv_n2_500 mtech_exf_iv_n2_750 mtech_exf_iv_n2_1000 mtech_exf_iv_n2_pop mtech_exf_iv_n1 
drop if year>2012 

*generate a few additional variables
gen weu=0 
replace weu=1 if region=="Western Europe and off-shoots" 
gen lagwe_mtech_exf_weu=lagwe_mtech_exf*weu
egen lagwe_mtech_av=mean(lagwe_mtech) if year>1959 & year<2011, by(country)
egen lagwe_lngdpc_av=mean(lagwe_lngdpc) if year>1959 & year<2011, by(country)
gen mtech_sa=mtech-mtech_exf
gen mtech_exf_weu=mtech_exf*weu
gen index_irt_exsa_weu=index_irt_exsa*weu

rename region2 region
rename continent2 continent

// Add labels
label variable country "Country name"
label variable iso3 "ISO3 country code"
label variable countryno "Numerical country identifier based on ISO3"
label variable ccode "Countries"
label variable year "Year"
label variable mtech "Military technology index (formative)"
label variable mtech_exf "Military tech. index ex. small arms (formative)"
label variable continent "Continent"
label variable region "Region"
label variable lngdpc "Log GDP per capita (Maddison)"
label variable democracy "Democracy dummy (Boix et al)"
label variable tpop "Total Population (CoW)"
label variable interstatewar "Interstate war, dummy(CoW)"
label variable civilwar "Civil war, dummy (CoW)"
label variable mtech_exf_iv_n2 "IV for mtech ex. small arms (baseline)"
label variable lagwe_mtech_exf_iv_n2_500 "IV ex. countries within 500km"
label variable lagwe_mtech_exf_iv_n2_750 "IV ex. countries within 750km"
label variable lagwe_mtech_exf_iv_n2_1000 "IV ex. countries within 1000km"
label variable lagwe_mtech_exf_iv_n2_pop "IV with population weights"
label variable lagwe_mtech_exf_iv_n1 "IV including neighbors"
label variable mtech_exf_iv_n2 "IV all"
label variable colony "Former colony dummy"
label variable colony_we "Former Western European colony dummy"
label variable colony_ussr "Former USSR state"
label variable colony_rest "Other colony (non WEU or USSR)"
label variable milex "Military expenditure (CoW)"
label variable lnmilex "Log military expenditure (CoW)"
label variable lnmilper "Log military personnel (CoW)"
label variable lagwe_mtech_exf "Arms technology lag"
label variable lagwe_mtech_s "Small arms lag"
label variable lagwe_lngdpc "Lag GDP per capita (log)"
label variable lagwe_mtech_exf_weu "Arms technology lag to Western Europe"
label variable lagwe_mtech "Arms technology lag to WEU (not exc small arms) (in years)"
label variable lagwe_ups "Lag to Western Europe in urban pop share (measured in years)"
label variable lagwe_naes "Lag to Western Europe in non-agricultural employment share"
label variable lagwe_schooling "Lag to Western Europe in educational attainment"
label variable census "First modern census"
label variable yearbook_published "First statistical yearbook published"
label variable ccode "Countries"
label variable lagwe_mtech_exf_iv_n2 "Distance weighted arms technology adoption (instrument)"
label variable m2_iv_n2 "Distance weighted adoption of automatic machine gun"
label variable r3_iv_n2 "Distance weighted adoption of 1st gen jets"
label variable distw2_index_irt_exsa "Distance-weighted IRT arms technology adoption (instrument)"
label variable mtech_exf "Arms Technology Index (formative)"
label variable mtech_sa "Small arms"
label variable mtech_exf_weu "Arms Technology Index x Western Europe"
label variable lngdp "GDP per capita (log)"
label variable index_irt_exsa "Arms Technology Index (IRT)"
label variable index_irt_exsa_weu "Arms Technology Index x Western Europe (IRT)"
label variable index_irt_sa "Small arms (IRT)"
label variable n1_democracy_avg "Share democracy in neighbor countries" 
label variable n2_democracy_avg "Share democracy in neighbors' neighbors"
label variable n1_interstatewar "War in neighboring countries" 
label variable n2_interstatewar "War in neighbors' neighbors"
label variable n1_civilwar "Civil war in neighboring countries" 
label variable n2_civilwar "Civil war in neighbors' neighbors"
label variable distw1_democracy "Distance-weighted polyarchy"
label variable distw1_interstatewar "Distance-weighted war"
label variable distw1_civilwar "Distance-weighted civil war"
label variable distw1_lngdp "Distance-weighted GDP pc (log)"
label variable interstatewar_3y "War in last three years"
label variable civilwar_3y "Civil war in last three years"
label variable ally_USA "US ally"
label variable ally_Russia "USSR ally"
label variable lntrade "Trade (log)"
label variable lnaid "Foreign aid (log)"
label variable le_total_oil_income_pc "Total oil income per capita (log)"
label variable le_total_resources_income_pc "Total natural resource p.c. (log)"
label variable independent "String specifies if country in year is independent" 
label variable weu "Western Europe and offshoots"
label variable coldwar "Dummy for cold war years (1947-1991)"
label variable lagwe_mtech_av "Arms technology lag to Western Europe (av. 1960-2010)"
label variable lagwe_lngdpc_av "Log GDP per capita lag to Western Europe (av. 1960-2010)"
label variable University "Year first university established in country"
label variable Diplomacy "Year country reaches diplomatic relations across four continents"
label variable aes "Agricultural employment share"
label variable naes "Nonagricultural employment share"
label variable schooling "Total yrs schooling, population 15-64"
label variable ups "Urban population share"

label variable adoptionyears1 "Adoption year matchlock musket"
label variable adoptionyears2 "Adoption year snaphaunce"
label variable adoptionyears3 "Adoption year flintlock musket"
label variable adoptionyears4 "Adoption year percussion luck musket"
label variable adoptionyears5 "Adoption year minié bullet rifle"
label variable adoptionyears6 "Adoption year breech-loading rifle"
label variable adoptionyears7 "Adoption year tubular magazine"
label variable adoptionyears8 "Adoption year box magazine"
label variable adoptionyears9 "Adoption year  assault rifle"
label variable adoptionyearm1 "Adoption year hand-cranked machine gun"
label variable adoptionyearm2 "Adoption year automatic machine gun"
label variable adoptionyeara1 "Adoption year field gun"
label variable adoptionyeara2 "Adoption year rifled artillery"
label variable adoptionyeara3 "Adoption year steel tube cannon"
label variable adoptionyeara4 "Adoption year breech-loading cannon"
label variable adoptionyeara5 "Adoption year recoil mechanism"
label variable adoptionyeart1 "Adoption year early tanks"
label variable adoptionyeart2 "Adoption year WWII tank"
label variable adoptionyeart3 "Adoption year 1st gen main battle tank"
label variable adoptionyeart4 "Adoption year 2nd gen main battle tank"
label variable adoptionyeart5 "Adoption year 3rd gen main battle tank"
label variable adoptionyearr1 "Adoption year early attack aircraft"
label variable adoptionyearr2 "Adoption year WWII attack aircraft"
label variable adoptionyearr3 "Adoption year 1st gen jet fighter"
label variable adoptionyearr4 "Adoption year 2nd gen jet fighter"
label variable adoptionyearr5 "Adoption year 3rd gen jet fighter"
label variable adoptionyearr6 "Adoption year 4th gen jet fighter"
label variable adoptionyearh1 "Adoption year 1st gen helicopter"
label variable adoptionyearh2 "Adoption year 2nd gen helicopter"
label variable adoptionyears1 "Year of first use matchlock musket"
label variable adoptionyears2 "Year of first use snaphaunce"
label variable adoptionyears3 "Year of first use flintlock musket"
label variable adoptionyears4 "Year of first use percussion luck musket"
label variable adoptionyears5 "Year of first use minié bullet rifle"
label variable adoptionyears6 "Year of first use breech-loading rifle"
label variable adoptionyears7 "Year of first use tubular magazine"
label variable adoptionyears8 "Year of first use box magazine"
label variable adoptionyears9 "Year of first use  assault rifle"
label variable adoptionyearm1 "Year of first use hand-cranked machine gun"
label variable adoptionyearm2 "Year of first use automatic machine gun"
label variable adoptionyeara1 "Year of first use field gun"
label variable adoptionyeara2 "Year of first use rifled artillery"
label variable adoptionyeara3 "Year of first use steel tube cannon"
label variable adoptionyeara4 "Year of first use breech-loading cannon"
label variable adoptionyeara5 "Year of first use recoil mechanism"
label variable adoptionyeart1 "Year of first use early tanks"
label variable adoptionyeart2 "Year of first use WWII tank"
label variable adoptionyeart3 "Year of first use 1st gen main battle tank"
label variable adoptionyeart4 "Year of first use 2nd gen main battle tank"
label variable adoptionyeart5 "Year of first use 3rd gen main battle tank"
label variable adoptionyearr1 "Year of first use early attack aircraft"
label variable adoptionyearr2 "Year of first use WWII attack aircraft"
label variable adoptionyearr3 "Year of first use 1st gen jet fighter"
label variable adoptionyearr4 "Year of first use 2nd gen jet fighter"
label variable adoptionyearr5 "Year of first use 3rd gen jet fighter"
label variable adoptionyearr6 "Year of first use 4th gen jet fighter"
label variable adoptionyearh1 "Year of first use 1st gen helicopter"
label variable adoptionyearh2 "Year of first use 2nd gen helicopter"
label variable firstuses1 "Year of first use matchlock musket"
label variable firstuses2 "Year of first use snaphaunce"
label variable firstuses3 "Year of first use flintlock musket"
label variable firstuses4 "Year of first use percussion luck musket"
label variable firstuses5 "Year of first use minié bullet rifle"
label variable firstuses6 "Year of first use breech-loading rifle"
label variable firstuses7 "Year of first use tubular magazine"
label variable firstuses8 "Year of first use box magazine"
label variable firstuses9 "Year of first use  assault rifle"
label variable firstusem1 "Year of first use hand-cranked machine gun"
label variable firstusem2 "Year of first use automatic machine gun"
label variable firstusea1 "Year of first use field gun"
label variable firstusea2 "Year of first use rifled artillery"
label variable firstusea3 "Year of first use steel tube cannon"
label variable firstusea4 "Year of first use breech-loading cannon"
label variable firstusea5 "Year of first use recoil mechanism"
label variable firstuset1 "Year of first use early tanks"
label variable firstuset2 "Year of first use WWII tank"
label variable firstuset3 "Year of first use 1st gen main battle tank"
label variable firstuset4 "Year of first use 2nd gen main battle tank"
label variable firstuset5 "Year of first use 3rd gen main battle tank"
label variable firstuser1 "Year of first use early attack aircraft"
label variable firstuser2 "Year of first use WWII attack aircraft"
label variable firstuser3 "Year of first use 1st gen jet fighter"
label variable firstuser4 "Year of first use 2nd gen jet fighter"
label variable firstuser5 "Year of first use 3rd gen jet fighter"
label variable firstuser6 "Year of first use 4th gen jet fighter"
label variable firstuseh1 "Year of first use 1st gen helicopter"
label variable firstuseh2 "Year of first use 2nd gen helicopter"
label variable s1_use "Matchlock in use"
label variable s2_use "Snaphaunce in use"
label variable s3_use "Flintlock in use"
label variable s4_use "Percussion lock in use"
label variable s5_use "Minié bullet rifle in use"
label variable s6_use "Breech-loading rifle in use"
label variable s7_use "Tubular magazine in use"
label variable s8_use "Box magazine in use"
label variable s9_use "Assault rifle in use"
label variable m1_use "Hand-cranked machine gun in use"
label variable m2_use "Automatic machine gun in use"
label variable a1_use "Field gun in use"
label variable a2_use "Rifled artillery in use"
label variable a3_use "Steel tubes in use"
label variable a4_use "Breech-loading cannon in use"
label variable a5_use "Recoil mechanism in use"
label variable t1_use "Early tank in use"
label variable t2_use "WWII tank in use"
label variable t3_use "1st gen main battle tank in use"
label variable t4_use "2nd gen main battle tank in use"
label variable t5_use "3rd gen main battle tank in use"
label variable r1_use "Early attack aircraft in use"
label variable r2_use "WWII attack aircraft in use"
label variable r3_use "1st gen jet fighter in use"
label variable r4_use "2nd gen jet fighter in use"
label variable r5_use "3rd gen jet fighter in use"
label variable r6_use "4th gen jet fighter in use"
label variable h1_use "1st gen helicopter in use"
label variable h2_use "2nd gen helicopter in use"

*rescaling
replace v2csreprss=v2csreprss*100
replace v2clkill=v2clkill*100 
replace v2x_pubcorr=v2x_pubcorr*100
replace v2x_corr=v2x_corr*100
replace v2x_execorr=v2x_execorr*100
replace v2clrspct=v2clrspct*100
replace v2x_polyarchy=v2x_polyarchy*100

*Order variables
order iso3 year ccode country* region continent* region weu mtech mtech_exf mtech_sa mtech_exf_weu lagwe_mtech lagwe_mtech_exf lagwe_mtech_s lagwe_mtech_exf_weu index_irt_exsa index_irt_sa index_irt_exsa_weu  mtech_exf_iv_n2 lagwe_mtech_exf_iv_n2_500 lagwe_mtech_exf_iv_n2_750 lagwe_mtech_exf_iv_n2_1000 lagwe_mtech_exf_iv_n2_pop lagwe_mtech_exf_iv_n1 mtech_exf_iv_n2 lngdp lagwe_lngdpc lagwe_naes lagwe_schooling lagwe_ups v2x_polyarchy v2x_pubcorr v2x_corr v2x_execorr v2* democracy
order n1_* n2_* distw1_* adoptionyear* firstuse* *_use, last


*save final data set and erase intermediate data sets generated by this do-file
save "coercive_imbalance_final/ci_main_jop.dta", replace


erase "coercive_imbalance_final/instruments_jop.dta"
erase "coercive_imbalance_final/diffusioncontrols_jop.dta"
erase "coercive_imbalance_final/neighbours_jop.dta"
erase "coercive_imbalance_final/temp_lags.dta"
erase "coercive_imbalance_final/temp_we.dta"

log close
